﻿using System;
using System.Collections;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Framework;
using DotNetNuke.Services.Localization;

namespace HeroldIT.Dnn.Modules
{
    /// <summary>
    /// A base class for views used in <see cref="PortalModuleBase"/> modules.
    /// </summary>
    public class PortalModuleView : UserControlBase, IModuleSettingsProvider
    {
        #region properties

        /// <summary>
        /// Gets or sets the base module.
        /// </summary>
        public PortalModuleBase ModuleBase { get; set; }

        /// <summary>
        /// Gets the module's configuration data.
        /// </summary>
        public ModuleInfo ModuleConfiguration
        {
            get { return null == this.ModuleBase ? null : this.ModuleBase.ModuleConfiguration; }
        }

        /// <summary>;
        /// Gets the module's settings.
        /// </summary>
        public Hashtable Settings
        {
            get { return null == this.ModuleBase ? null : this.ModuleBase.Settings; }
        }

        /// <summary>
        /// Gets whether the module is editable by the current user.
        /// </summary>
        protected bool IsEditable
        {
            get { return null == this.ModuleBase ? false : this.ModuleBase.IsEditable; }
        }

        private string localResourceFile;

        /// <summary>
        /// The path to the local resource file
        /// </summary>
        public string LocalResourceFile
        {
            get
            {
                if (String.IsNullOrEmpty(this.localResourceFile))
                {
                    return (this.TemplateSourceDirectory + "/App_LocalResources/" + this.AppRelativeVirtualPath.Substring(this.AppRelativeVirtualPath.LastIndexOf('/') + 1));
                }
                return this.localResourceFile;
            }
            set
            {
                this.localResourceFile = value;
            }
        }

        #endregion

        #region methods

        /// <summary>
        /// Gets the localized string for the given key.
        /// </summary>
        /// <param name="key">The key for which to find the localized string.</param>
        /// <returns>The localized string for the given key.</returns>
        protected string GetString(string key)
        {
            return Localization.GetString(key, this.LocalResourceFile);
        }

        /// <summary>
        /// Gets the edit url for the module.
        /// </summary>
        /// <returns>The edit url for the module.</returns>
        protected string EditUrl()
        {
            return null == this.ModuleBase ? null : this.ModuleBase.EditUrl();
        }

        /// <summary>
        /// Gets the edit url for the module.
        /// </summary>
        /// <param name="controlKey">The key of the control to use.</param>
        /// <returns>The edit url for the module.</returns>
        protected string EditUrl(string controlKey)
        {
            return null == this.ModuleBase ? null : this.ModuleBase.EditUrl(controlKey);
        }

        /// <summary>
        /// Gets the edit url for the module.
        /// </summary>
        /// <param name="keyName">The name of the parameter for the item's key.</param>
        /// <param name="keyValue">The value of the item's key.</param>
        /// <returns>The edit url for the module.</returns>
        protected string EditUrl(string keyName, string keyValue)
        {
            return null == this.ModuleBase ? null : this.ModuleBase.EditUrl(keyName, keyValue);
        }

        /// <summary>
        /// Gets the edit url for the module.
        /// </summary>
        /// <param name="keyName">The name of the parameter for the item's key.</param>
        /// <param name="keyValue">The value of the item's key.</param>
        /// <param name="controlKey">The key of the control to use.</param>
        /// <returns>The edit url for the module.</returns>
        protected string EditUrl(string keyName, string keyValue, string controlKey)
        {
            return null == this.ModuleBase ? null : this.ModuleBase.EditUrl(keyName, keyValue, controlKey);
        }

        /// <summary>
        /// Gets the edit url for the module.
        /// </summary>
        /// <param name="keyName">The name of the parameter for the item's key.</param>
        /// <param name="keyValue">The value of the item's key.</param>
        /// <param name="controlKey">The key of the control to use.</param>
        /// <param name="additionalParameters">Additional parameters to add to the url.</param>
        /// <returns>The edit url for the module.</returns>
        protected string EditUrl(string keyName, string keyValue, string controlKey, params string[] additionalParameters)
        {
            return null == this.ModuleBase ? null : this.ModuleBase.EditUrl(keyName, keyValue, controlKey, additionalParameters);
        }

        #endregion
    }

    /// <summary>
    /// A base class for views used in <see cref="PortalModuleBase"/> modules.
    /// </summary>
    public class PortalModuleView<T> : PortalModuleView
        where T : PortalModuleBase
    {
        /// <summary>
        /// Gets or sets the base module.
        /// </summary>
        public new T ModuleBase
        {
            get
            {
                return base.ModuleBase as T;
            }
            set
            {
                base.ModuleBase = value;
            }
        }
    }
}